#include <windows.h>
#include <iostream>
#include <stdio.h>
#include <cmath>


using namespace std;

typedef int CF_LONGWord;
typedef UCHAR CF_Byte;
typedef short CF_Word;

void setBit(CF_LONGWord &number, int pos);
void clearBit(CF_LONGWord &number, int pos);
void setBit(CF_LONGWord &number, int pos, bool value);
bool getBit(CF_LONGWord number, int pos);
CF_LONGWord getBits(CF_LONGWord number, int hpos, int lpos);
void setBits(CF_LONGWord &number, int hpos, int lpos, CF_LONGWord value);
CF_Byte getByte(CF_LONGWord number, int pos);
CF_Word getWord(CF_LONGWord number, int pos);
void setByte(CF_LONGWord &number, int pos, CF_Byte value);
void setWord(CF_LONGWord &number, int pos, CF_Word value);

int main()
{
    int num = 358447321;
    setWord(num, 1, 58756);
    printf ("ending vals = %d \n", num);
   return 0;
}
void setBit(CF_LONGWord &number, int pos)
{
       int t = pow(2,pos);
       number |=t;
       printf ("Set %d \n",number);
}
void clearBit(CF_LONGWord &number, int pos)
{
    int t = pow(2,pos);
    int inv = ~t;
    number &=inv;
    printf ("Cleared %d \n", number);
}
void setBit(CF_LONGWord &number, int pos, bool value)
{
    if (value==1)
    {
        setBit(number, pos);
    }
    else
    {
        clearBit(number,pos);
    }

}
bool getBit(CF_LONGWord number, int pos)
{
   number = number >> pos;

   if(number % 2 ==0)
    {
		printf("THE BIT IS = 0 \n");
        return false;
    }
    else
    {
		printf("THE BIT IS = 1 \n");
        return true;
    }

}
CF_LONGWord getBits(CF_LONGWord number, int hpos, int lpos)
{
     int temp=0;
     number = number >> lpos;
     printf("AFTER SHIFT = %d \n", number);
     hpos=hpos-lpos;
     while(hpos >= 0)

     {
         temp = temp+(pow(2,hpos));
         hpos=hpos-1;
         printf("ENTERED\n");
         printf("temp in the loop = %d \n", temp);
     }
     printf("TEMP IS = %d\n", temp);
     number = number & temp;

     printf ("The bits are %d \n", number);
     return number;
}

void setBits(CF_LONGWord &number, int hpos, int lpos, CF_LONGWord value)
{
    int mini=lpos;
    for(int i=0; i<=(hpos-mini); i++)
    {
        if (getBit(value, i)==false)
        {
            clearBit(number, lpos);
            lpos++;
        }
        else
        {
            setBit(number, lpos);
            lpos++;
        }

    }
}
CF_Byte getByte(CF_LONGWord number, int pos)
{
    number = number >> (8*pos);
    printf("The byte is = %d \n", (number & 0xFF));
    return (number & 0xFF);
}

CF_Word getWord(CF_LONGWord number, int pos)
{
    number = number >> (16*pos);
    printf("The word is = %d \n", number & 0xFFFF);
    return (number & 0xFFFF);
}

void setByte(CF_LONGWord &number, int pos, CF_Byte value)
{
    setBits(number, ((pos*8)+7), (pos*8), value);
}

void setWord(CF_LONGWord &number, int pos, CF_Word value)
{
    setBits(number, ((pos*16)+15), (pos*16), value);
}

